﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DataAccess;
using Common;
using Entities;
using System.Collections;
using System.IO;
using System.Windows.Forms;

namespace BusinessLogic
{

    public class SaoLuuHeThong
    {
        void LuuViet(string hanhDong, string maNhanVien, string noiDung, string tenDangNhap, string thoiGianThucHien)
        {
            try
            {
                Entities.LuuVietHeThong lv = new Entities.LuuVietHeThong();
                lv.HanhDong = hanhDong;
                lv.MaNhanVien = maNhanVien;
                lv.NoiDung = noiDung;
                lv.TenDangNhap = tenDangNhap;
                lv.ThoiGianThucHien = thoiGianThucHien;
                new BusinessLogic.LuuVietHeThong().LuuViet(lv);
            }
            catch
            {
                return;
            }
        }

        
        private SqlCommand cmd;
        private ArrayList arr;
        private SqlConnection cn;
        public bool BackUp()
        {
            try
            {
                string path = "C:\\BackUp";
                DirectoryInfo FI = new DirectoryInfo(path);
                string fileName = "BUSuperMarket" + DateTime.Now.ToShortDateString().Replace('/', '-');
                string sql = "BACKUP DATABASE QuanLySieuThi TO DISK='" + path + "\\" + fileName + "'";
                try
                {
                    File.Delete(path + "\\" + fileName);

                }
                catch
                {
                }
                if (!FI.Exists)
                {
                    FI.Create();
                }

                
                cmd = new SqlCommand();
                cmd.CommandText = sql;
                cmd.Connection = DataAccess.Connection.OpenConnection();
                cmd.ExecuteNonQuery();
                cmd.Connection.Dispose();
                cmd.Connection.Close();
                cmd.Dispose();
                return true;
            }
            catch
            { return false; }
            finally
            {
                cmd.Connection.Dispose();
                cmd.Connection.Close();
                cmd.Dispose();
                
            }
        }
        public Entities.SaoLuuHeThong[] LayBackUp()
        {
            try
            {
                string path = "C:\\BackUp";
                DirectoryInfo DI = new DirectoryInfo(path);
                if (!DI.Exists)
                {
                    return null;
                }
                FileInfo[] FI = DI.GetFiles("BUSuperMarket*");
                Entities.SaoLuuHeThong[] slht = new Entities.SaoLuuHeThong[FI.Length];
                int j = 0;
                for (int i = FI.Length - 1; i >= 0; i--)
                {
                    slht[j] = new Entities.SaoLuuHeThong(FI[i].Name, FI[i].LastWriteTime.ToShortDateString(), FI[i].Length);
                    j++;
                }
                return slht;
            }
            catch
            {
                return null;
            }
        }

        public bool Restore(Entities.SaoLuuHeThong slht1)
        {
            try
            {
                try
                {
                    SqlConnection.ClearAllPools();
                    string path = "C:\\BackUp";
                    DirectoryInfo FI = new DirectoryInfo(path);
                    string fileName = "BUSuperMarket" + slht1.Name;
                    string sql = "ALTER DATABASE QuanLySieuThi ";
                    sql += "SET SINGLE_USER WITH ROLLBACK IMMEDIATE ";
                    sql += " use master ";
                    sql += "restore DATABASE QuanLySieuThi from DISK='" + path + "\\" + slht1.Name + "' with replace";
                    sql += " ALTER DATABASE SupermarketManagement ";
                    sql += "SET MULTI_USER";

                    if (!FI.Exists)
                    {
                        return false;
                    }

                    cmd = new SqlCommand();
                    cmd.CommandText = sql;
                    cmd.Connection = DataAccess.Connection.OpenConnection();

                    cmd.ExecuteNonQuery();
                }
                catch
                { return false; }
                finally
                {
                    cmd.Connection.Dispose();
                    cmd.Connection.Close();
                    cmd.Dispose();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
